package layouts;

import java.awt.*;
import java.util.HashMap;

public class TotalPercentLayout extends PartialPercentLayout {
	private HashMap<Component, Dimension> sizes;
	
	public TotalPercentLayout() {
		super();
		
		sizes = new HashMap<Component, Dimension>();
	}
    
	@Override
	public void layoutContainer(Container parent) {
		Component[] components = parent.getComponents();
		int parentWidth = parent.getWidth();
		int parentHeight = parent.getHeight();
		
		for(int i = 0; i < components.length; i++) {
			Component component = components[i];
			
			if(fill) {
				Point location = component.getLocation();
				
				if(location.getX() < 0)
					location.x = 0;
					
				if(location.getX() > 100)
					location.x = 100;
					
				if(location.getY() < 0)
					location.y = 0;
					
				if(location.getY() > 100)
					location.y = 100;
				
				Dimension size = component.getSize();
				
				if(size.getWidth() < 0)
					size.width = 0;
					
				if(size.getWidth() > 100)
					size.width = 100;
					
				if(size.getHeight() < 0)
					size.height = 0;
					
				if(size.getHeight() > 100)
					size.height = 100;
					
				locations.put(component, location);
				sizes.put(component, size);
			}
			
			int width = (int) (parentWidth * (sizes.get(component).getWidth() / 100));
			int height = (int) (parentHeight * (sizes.get(component).getHeight() / 100));
			int x = (int) ((parentWidth - width) * (locations.get(component).getX() / 100));
			int y = (int) ((parentHeight - height) * (locations.get(component).getY() / 100));
			
			component.setSize(width, height);
			component.setLocation(x, y);
		}
		
		fill = false;
	}
	
	@Override
	public void removeLayoutComponent(Component comp) {
		super.removeLayoutComponent(comp);
		
		sizes.remove(comp);
	}
}
